<?php
namespace admin\widgets;

use yii\base\Widget;
use yii\helpers\Html;

class AdminThumbsWidget extends Widget {

    public $model = [];
    public $field = 'getThumb';
    public $col = 6;
    public $caption;

    public function run() {
        $thumbs = [];
        if (empty($this->items)) {
            return '';
        }

        foreach ($this->items as $item) {
            if (is_string($item)) {
                $src = $item;
            } else {
                $field = $this->field;
                $src = is_object($item) ? $item->$field() : $item[$field];
            }
            if ($this->caption) {
                $label = $item[$this->caption['label']];
                $p = $item[$this->caption['p']];
                $caption = Html::tag('div', "<h3>{$label}</h3><p>{$p}</p>", ['class' => 'caption']);
                $html = Html::tag('div', Html::img($src) . $caption, ['class' => 'thumbnail']);
            } else {
                $html = Html::a(Html::img($src), $src, ['target' => '_blank', 'class' => 'thumbnail']);
            }

            if ($this->col > 0) {
                $html = Html::tag('div', $html, ['class' => "col-xs-{$this->col} col-md-{$this->col}"]);
            }
            $thumbs[] = $html;
        }
        return Html::tag('div', implode("\n", $thumbs), ['class' => 'row']);
    }
}